package com.csdj.t309.geamtest.config;

import com.csdj.t309.geamtest.common.CacheConstant;
import com.csdj.t309.geamtest.entity.User;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.TouchedExpiryPolicy;
import javax.cache.spi.CachingProvider;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * 缓存配置类
 */
@Configuration
@EnableCaching
public class CacheConfig {

    /*@Bean*/
    public CacheManager manager(){
        //用户缓存配置
        CacheConfiguration<Integer, User> configBuilder =
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, User.class, ResourcePoolsBuilder.heap(10))
                        .build();

        //创建缓存
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache(CacheConstant.CACHE_NAME_USER, configBuilder)
                .build(true);

        return cacheManager;
    }

    @Bean
    public javax.cache.CacheManager jCacheManager(){
        //创建缓存供应者
        CachingProvider provider = Caching.getCachingProvider();
        //创建缓存管理者
        javax.cache.CacheManager cacheManager = provider.getCacheManager();
        //配置缓存相关信息
        MutableConfiguration<Object,Object> userConfig = new MutableConfiguration<Object, Object>()
                .setTypes(Object.class,Object.class)//配置数据类型
                .setStoreByValue(false)//是否保存到磁盘
                .setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new javax.cache.expiry.Duration(TimeUnit.SECONDS,30)));//设置空闲时间为60秒
        //创建缓存分区
        cacheManager.createCache(CacheConstant.CACHE_NAME_USER,userConfig);
        //返回给spring容器
        return cacheManager;


    }

    /*@Bean*/
    public RedisCacheManager redisCacheManager(RedisConnectionFactory factory, RedisSerializer<Object> serializer){
        RedisCacheConfiguration userConf = RedisCacheConfiguration.defaultCacheConfig();
        //配置各项内容
        userConf = userConf.disableCachingNullValues();
        userConf = userConf.entryTtl(Duration.ofSeconds(300));
        userConf = userConf.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        userConf = userConf.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
        RedisCacheManager.RedisCacheManagerBuilder builder =
                RedisCacheManager.builder(factory)
                        .withCacheConfiguration(CacheConstant.CACHE_NAME_USER,userConf);
        return builder.build();
    }

}